package transporte.web;

import java.beans.PropertyEditorSupport;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TimestampDateEditor extends PropertyEditorSupport {
	
	private final DateFormat dateFormat;
	private final String defaultFormat = "MM/dd/yyyy";
	private static final org.apache.commons.logging.Log logger = org.apache.commons.logging.LogFactory.getLog(TimestampDateEditor.class);

	public TimestampDateEditor()
	{
		this.dateFormat = new SimpleDateFormat(defaultFormat);
	}

	/**
	 * Parse the Date from the given text, using the specified DateFormat.
	 */
	public void setAsText(String datestr) throws IllegalArgumentException
	{

		if (datestr == null || datestr.trim().length() <= 0) {
			setValue(null);
			return;
		}

		//GregorianCalendar currCal = new GregorianCalendar();
		//int year = currCal.get(Calendar.YEAR);

		Date parseddate = null;

		// default format
		if (this.dateFormat != null) {
			try {
				parseddate = this.dateFormat.parse(datestr);
			}
			catch (ParseException pe) {
				if (logger.isDebugEnabled()) logger.debug(pe);
			}
		}
		
		/*//format 1
		SimpleDateFormat sdf = null;
		if (parseddate == null) {
			sdf = new SimpleDateFormat("MM/dd/yy");
	
			try {
				parseddate = sdf.parse(datestr);
			}
			catch (ParseException pe) {
				parseddate = null; // try next format
				if (logger.isDebugEnabled()) logger.debug(pe);
			}
		}

		//format 1
		if (parseddate == null) {
			sdf = new SimpleDateFormat("MM-dd-yy");

			try {
				parseddate = sdf.parse(datestr);
			}
			catch (ParseException pe) {
				parseddate = null; // try next format
			}
		}

		if (parseddate == null) {
			//format 1
			sdf = new SimpleDateFormat("MM/dd/yyyy");

			try {
				if (datestr.indexOf("/") >= 0 && datestr.indexOf("/") == datestr.lastIndexOf("/")) {
					datestr = datestr.trim() + "/" + year;
				}
				parseddate = sdf.parse(datestr);
			}
			catch (ParseException pe) {
				parseddate = null; // try next format
				if (logger.isDebugEnabled()) logger.debug(pe);
			}
		}

		if (parseddate == null) {
			//format 1
			sdf = new SimpleDateFormat("dd-MMM-yyyy");

			try {
				if (datestr.indexOf("/") >= 0 && datestr.indexOf("/") == datestr.lastIndexOf("/")) {
					datestr = datestr.trim() + "/" + year;
				}
				parseddate = sdf.parse(datestr);
			}
			catch (ParseException pe) {
				parseddate = null; // try next format
				if (logger.isDebugEnabled()) logger.debug(pe);
			}
		}

		//format 2
		if (parseddate == null) {
			sdf = new SimpleDateFormat("MMM dd, yyyy");
			try {
				parseddate = sdf.parse(datestr);
			}
			catch (ParseException pe) {
				parseddate = null; // try next format
				if (logger.isDebugEnabled()) logger.debug(pe);
			}
		}

		//format 3
		if (parseddate == null) {
			sdf = new SimpleDateFormat("MMM dd,yyyy");
			try {
				parseddate = sdf.parse(datestr);
			}
			catch (ParseException pe) {
				parseddate = null; // try next format
				if (logger.isDebugEnabled()) logger.debug(pe);
			}
		}

		//format 4
		if (parseddate == null) {
			sdf = new SimpleDateFormat("MMM dd yyyy");
			try {
				if (datestr.indexOf(" ") >= 0 && datestr.indexOf(" ") == datestr.lastIndexOf(" ")) {
					datestr = datestr.trim() + " " + year;
				}
				parseddate = sdf.parse(datestr);
			}
			catch (ParseException pe) {
				parseddate = null; // try next format
				if (logger.isDebugEnabled()) logger.debug(pe);
			}
		}

		//format 4
		if (parseddate == null) {
			sdf = new SimpleDateFormat("MMM. dd, yyyy");
			try {
				if (datestr.indexOf(" ") >= 0 && datestr.indexOf(" ") == datestr.lastIndexOf(" ")) {
					datestr = datestr.trim() + " " + year;
				}
				parseddate = sdf.parse(datestr);
			}
			catch (ParseException pe) {
				parseddate = null; // try next format
				if (logger.isDebugEnabled()) logger.debug(pe);
			}
		}
		//format 5
		if (parseddate == null) {
			sdf = new SimpleDateFormat("dd MMM yyyy");
			try {
				if (datestr.indexOf(" ") >= 0 && datestr.indexOf(" ") == datestr.lastIndexOf(" ")) {
					datestr = datestr.trim() + " " + year;
				}
				parseddate = sdf.parse(datestr);
			}
			catch (ParseException pe) {
				if (logger.isDebugEnabled()) logger.debug(pe);
				//no valid format, throw exception
				//throw pe;
			}
		}*/

		if (logger.isDebugEnabled()) logger.debug("What is in the get on the  date editor ?, this: " + parseddate); 
			
		if (parseddate != null) {
			setValue(new Timestamp(parseddate.getTime()));
		}
		
	}

	/**
	 * Format the Date as String, using the specified DateFormat.
	 */
	public String getAsText()
	{
		Timestamp value = (Timestamp) getValue();
		String returnString = value != null ? this.dateFormat.format(value) : "";
		if (logger.isDebugEnabled()) logger.debug("What is in the date editor ?, this: " + returnString); 
		return returnString;
	}
	
}
